package lab3;

public class SynchronizedTest extends Thread{
    String s;
    Object myLock;
    Object nextLock;

    SynchronizedTest(String s, Object lock, Object nextLock){
        this.s = s;
        this.myLock = lock;
        this.nextLock = nextLock;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (myLock) {
                synchronized (nextLock) {
                    nextLock.notify();
                    System.out.println(s);
                }
                try {
                    myLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        Object lock1 = new Object();
        Object lock2 = new Object();
        Object lock3 = new Object();
        SynchronizedTest t1 = new SynchronizedTest("甲", lock1,lock2);
        SynchronizedTest t2 = new SynchronizedTest("乙", lock2,lock3);
        SynchronizedTest t3 = new SynchronizedTest("丙", lock3,lock1);
        t1.start();
        sleep(100);
        t2.start();
        sleep(100);
        t3.start();
    }
}
